package services

import beans.Comment
import beans.Reply
import base.Message
import base.State
import common.permission.Permission
import common.permission.PermissionManager
import controllers.CommentController
import models.CommentM
import models.ReplyM
import models.UserInfoM
import models.UserM
import java.sql.Timestamp

class CommentService(controller: CommentController):
        BaseService<CommentController>(controller){

    fun createMessage(userId: Int, recordId: Int, content: String){
        if (!checkCommentPermission(userId)) return
        val model = CommentM()
        model[CommentM.USER_ID] = userId
        model[CommentM.RECORD_ID] = recordId
        model[CommentM.CONTENT] = content
        model[CommentM.CREATE_TIME] = Timestamp(System.currentTimeMillis())
        if (model.save()){
            val message = Message(State.STATE_SUCCESS, data = "")
            controller.renderJson(message)
        } else {
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_SERVER_INSIDE_ERROR))
            controller.renderJson(message)
        }
    }

    private fun checkCommentPermission(userId: Int): Boolean{
        if (!PermissionManager.checkUserPermission(userId, Permission.COMMENT)){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_COMMENT_NO_PERMISSION))
            controller.renderJson(message)
            return false
        }
        return true
    }

    fun createSubMessage(msgId: Int, fromId: Int, toId: Int, content: String){
        if (!checkCommentPermission(fromId)) return
        val model = ReplyM()
        model[ReplyM.LV_MSG_ID] = msgId
        model[ReplyM.FROM_USER_ID] = fromId
        model[ReplyM.TO_USER_ID] = toId
        model[ReplyM.CONTENT] = content
        model[ReplyM.CRATE_TIME] = Timestamp(System.currentTimeMillis())
        if (model.save()){
            val message = Message(State.STATE_SUCCESS, data = "")
            controller.renderJson(message)
        } else {
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_SERVER_INSIDE_ERROR))
            controller.renderJson(message)
        }
    }

    fun loadComment(recordId: Int){
        val comments = CommentM.findByRecordId(recordId)
        /*if (comments.isEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_COMMENT_COMMENT_FAIL))
            controller.renderJson(message)
            return
        }*/
        val commentList = comments.map {
            val userId = it.getInt(CommentM.USER_ID)
            val userM = UserM.AGENT.findById(userId)
            val userInfoM = UserInfoM.findInfobyUserId(userId)
            val commentId = it.getInt(CommentM.ID)
            val replyList = ReplyM.findByCommentId(commentId).map { model ->
                val from = UserM.AGENT.findById(model.getInt(ReplyM.FROM_USER_ID))
                val to = UserM.AGENT.findById(model.getInt(ReplyM.TO_USER_ID))
                val reply = Reply.from(model, hashMapOf(
                        "from_username" to from[UserM.USERNAME],
                        "to_username" to to[UserM.USERNAME]
                ))
                reply
            }
            val comment = Comment.from(it, hashMapOf(
                    "username" to userM[UserM.USERNAME],
                    "avatar" to userInfoM[UserInfoM.AVATAR]
            ))
            comment.reply = replyList
            comment
        }
        val message = Message(State.STATE_SUCCESS, data = commentList)
        controller.renderJson(message)
    }

    fun deleteComment(userId: Int, commentId: Int) {
        val commentM = CommentM.AGENT.findById(commentId)
        if (commentM != null && commentM.getInt(CommentM.USER_ID) == userId && commentM.delete()){
            val message = Message(State.STATE_SUCCESS, data = "")
            controller.renderJson(message)
        } else {
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_COMMENT_DELETE_FAIL))
            controller.renderJson(message)
        }
    }

    fun deleteReply(userId: Int, replyId: Int){
        val replyM = ReplyM.AGENT.findById(replyId)
        if (replyM != null && replyM.getInt(ReplyM.FROM_USER_ID) == userId && replyM.delete()){
            val message = Message(State.STATE_SUCCESS, data = "")
            controller.renderJson(message)
        } else {
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_COMMENT_DELETE_FAIL))
            controller.renderJson(message)
        }
    }

}